package sdcncsi.ict.counter;

import java.awt.Color;
import java.util.Random;

public class ColorTool {

	/**
	 * 给定范围获得随机颜色，由指定范围分别随机生成R、G、B值
	 * @param fore
	 * @param back
	 * @return Color(R, G, B)
	 */
	public static Color getRandColor(int fore, int back) {
		Random random = new Random(System.currentTimeMillis());
		if (fore > 255 || fore < 0)
			fore = 0;
		if (back < 0 || back > 255)
			back = 255;
		int r = fore + random.nextInt(back - fore);
		int g = fore + random.nextInt(back - fore);
		int b = fore + random.nextInt(back - fore);
		return new Color(r, g, b);
	}
	
	/**
	 * 给定范围获得随机颜色，由指定范围分别随机生成R、G、B值
	 * @param fore
	 * @param back
	 * @return String "#RRGGBB"
	 */
	public static String getRandColorString(int fore, int back) {
		Random random = new Random(System.currentTimeMillis());
		if (fore > 255 || fore < 0)
			fore = 0;
		if (back < 0 || back > 255)
			back = 255;
		int r = fore + random.nextInt(back - fore);
		int g = fore + random.nextInt(back - fore);
		int b = fore + random.nextInt(back - fore);
		StringBuffer colorStrBuf = new StringBuffer("#");
		if(r<16) colorStrBuf.append('0');
		colorStrBuf.append(Integer.toHexString(r));
		if(g<16) colorStrBuf.append('0');
		colorStrBuf.append(Integer.toHexString(g));
		if(b<16) colorStrBuf.append('0');
		colorStrBuf.append(Integer.toHexString(b));
		return colorStrBuf.toString();
	}
	
}
